<?php

namespace App\Http\Controllers\VideoAdmin;

use App\Http\Controllers\Admin\BaseController;
use App\Library\UpTools;
use App\Models\video\BroadcasterModel;
use App\Models\video\CommentModel;
use App\Models\video\DynamicModel;
use App\Models\video\ReplyModel;
use App\Models\video\VodVideoModel;
use Illuminate\Http\Request;

class DynamicController extends BaseController
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    // 获取动态列表
    public function getDynamicList(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $page = $request->page ?: 1;
        $pageSize = $request->pageSize ?: 10;
        $db = DynamicModel::query()->from(DynamicModel::query()->getModel()->getTable()." as d")
            ->leftJoin(VodVideoModel::query()->getModel()->getTable()." as v", "v.id", "=", "d.belong_id")
            ->where(["d.mall_id" => $request->mallData["id"], "d.status" => 1]);
        $data['count'] = $db->count();
        $data['currentPage'] = $page;
        $data['totalPage'] = ceil(($data['count'])/$pageSize);
        $database = $db->select([
            "d.id","d.broadcaster_id","d.belong_id","d.contents","d.pic_id","d.praise_num","d.type","d.create_time",//"v.title","v.view_count"
        ])->skip(($page - 1) * $pageSize)->take($pageSize)->orderBy("d.create_time", "desc")->get();
        if($database){
            $database = UpTools::transferFields($database->toArray());
            foreach ($database as $k => $item){
                $data["list"][$k] = [
                    "id" => $item["id"], "contents" => $item["contents"], "praiseNum" => $item["praiseNum"], "type" => $item["type"], "publishTime" => $item["createTime"],
                    "broadcasterId" => $item["broadcasterId"]
                ];
                if(empty($userTempData[$item["broadcasterId"]])){
                    $userTempData[$item["broadcasterId"]] = BroadcasterModel::getFields("name", ["id" => $item["broadcasterId"]], true, false);
                }
                $data["list"][$k]["userName"] = !empty($userTempData[$item["broadcasterId"]]["name"]) ? $userTempData[$item["broadcasterId"]]["name"] : "";
                if($item["type"] == '2'){ // 2视频
                    $data["list"][$k] = array_merge($data["list"][$k], ["videoId" => $item["belongId"]]);
                }else{ // 图文
                    if(!empty($item["picId"])) {
                        if (strpos($item["picId"], ",") !== false) {
                            $data["list"][$k]["picUrl"] = explode(",", $item["picId"]);
                            foreach ($data["list"][$k]["picUrl"] as &$i) {
                                $i = UpTools::processingPictures($i);
                            }
                        } else {
                            $data["list"][$k]["picUrl"][] = UpTools::processingPictures($item["picId"]);
                        }
                    }
                }
                $data["list"][$k]["commentCount"] = CommentModel::getCount(["belong_id" => $item["id"], "type" => 3, "status" => 1, "mall_id" => $request->mallData["id"]]) +
                    ReplyModel::query()->from(ReplyModel::query()->getModel()->getTable(). " as r")
                        ->leftJoin(CommentModel::query()->getModel()->getTable()." as c", "c.id", '=', "r.comment_id")
                        ->where(["c.belong_id" => $item["id"], "r.status" => 1])
                        ->count();
            }
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 删除动态
    public function delDynamic(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        DynamicModel::delByFilter(["id" => $request->id]);
        return $this->return_result($this->returnData);
    }
}
